package com.ittx.dzt.tiktok.service;

import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.sys.response.Result;
import com.ittx.dzt.tiktok.constant.TiktokApiConstant;
import com.ittx.dzt.tiktok.handler.AbstractBaseTiktokApiHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 根据delivery_option获取快递公司列表
 * <p>
 * cy.carrierPlatform.download
 * /logistics/202309/delivery_options/{delivery_option_id}/shipping_providers (查询承运商)  https://partner.tiktokshop.com/docv2/page/650aa48d4a0bb702c06d85cd?external_id=650aa48d4a0bb702c06d85cd
 * </p>>
 *
 * @author gywang
 * @since 2024-07-23
 */
@Slf4j
@Service
public class TiktokCarrierSyncService extends AbstractBaseTiktokApiHandler<String> {
    @Override
    public String getTiktokMethod() {
        return TiktokApiConstant.SHIPPING_PROVIDERS_LIST;
    }

    @Override
    public String getLocalApiMethod() {
        return open.constant.PlatformApiConstant.InboundInterface.CARRIER_PLATFORM_DOWNLOAD;
    }

    @Override
    public Result doProcess(PlatformApiParamDTO platformApiParamDTO, String deliveryOptionId) {
        Result<?> validResult = checkShopCipherInfo(platformApiParamDTO);
        if (validResult.hasError()) {
            return validResult;
        }
        String url = getDefaultUrl(platformApiParamDTO.getStoreConfig());
        url = StringUtils.replace(url, "{delivery_option_id}", deliveryOptionId);
        Map<String, Object> params = new HashMap<>();
        params.put("shop_cipher", getShopCipher(platformApiParamDTO));
        Result result = this.executeTiktokApi(url, platformApiParamDTO.getStoreConfig(), params, null, platformApiParamDTO.getRequestId(), TiktokApiConstant.ApiHttpMethod.GET);
        if (!result.isSuccess()) {
            return result;
        }
        Map<String, Object> resultMap = (Map<String, Object>) result.getData();
        return this.sendRequestToPlatform(platformApiParamDTO, resultMap, true);
    }
}
